<?php

namespace App\Http\Service;

use App\Http\Model\AreaModel;
use App\Http\Model\Region5Model;
use App\Http\Model\TempAreaModel;
use App\Http\Service\CommonService;
use Illuminate\Support\Facades\DB;
use phpspider\core\requests;
use phpspider\core\selector;

class RegionService {

    public static function region() {
        DB::beginTransaction();
        try {
            $url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/index.html";

//            $url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/11.html";
//            requests::$input_encoding = 'gb2312';
//            requests::$output_encoding = 'utf-8';
//            requests::set_header("Cookie", "_trs_uv=junnrn8e_6_67ou; AD_RS_COOKIE=20082855; wzws_cid=c59f051a3b6967bb4c9c5ebef4e77b9aba0136d0de579676e0eb8797bb3ebe233a62198cd32e4852d598f7be70171c2aefdbf0f68179dc959437c0efd734393f");
//            requests::set_header("Host", "www.stats.gov.cn");
//            requests::set_header("Upgrade-Insecure-Requests", 1);
//            requests::set_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36");
//            requests::set_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
//            requests::set_header("Referer", "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/");
//            requests::set_header("Accept-Language", "zh-CN,zh;q=0.9");
//            requests::set_header("Host", "www.stats.gov.cn");
//
//            $html = requests::get($url);
//
//            CommonService::p($html);
//            $data = selector::select($html, ".provincetr", "css");

            $request = \PhantomJs::get($url);

            $response = \PhantomJs::send($request);

            if ($response->getStatus() === 200) {

                // Dump the requested page content
                echo $response->getContent();
            }


            CommonService::p($data);
            DB::commit();
            return true;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

}
